//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartGridView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartGridView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ID_GRID	2

/////////////////////////////////////////////////////////////////////////////
// CChartGridView

IMPLEMENT_DYNCREATE(CChartGridView, CBCGPChartExampleView)

CChartGridView::CChartGridView()
	: CBCGPChartExampleView(CChartGridView::IDD)
{
	//{{AFX_DATA_INIT(CChartGridView)
	m_nChartType = 0;
	//}}AFX_DATA_INIT
}

CChartGridView::~CChartGridView()
{
}

void CChartGridView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartGridView)
	DDX_Control(pDX, IDC_CHART_TYPE, m_wndChartType);
	DDX_Control(pDX, IDC_GRID, m_wndGridLocation);
	DDX_CBIndex(pDX, IDC_CHART_TYPE, m_nChartType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartGridView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartGridView)
	ON_CBN_SELENDOK(IDC_CHART_TYPE, OnChartType)
	//}}AFX_MSG_MAP
	ON_REGISTERED_MESSAGE(BCGM_GRID_ITEM_CHANGED, OnGridDataChanged)
	ON_UPDATE_COMMAND_UI(ID_COLOR_THEME_COMBO, OnUpdateUnusedCommand)
	ON_UPDATE_COMMAND_UI(ID_CHART_EXPORT, OnUpdateUnusedCommand)
	ON_UPDATE_COMMAND_UI(ID_CHART_COPY, OnUpdateUnusedCommand)
	ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateUnusedCommand)
	ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_PREVIEW, OnUpdateUnusedCommand)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartGridView diagnostics

#ifdef _DEBUG
void CChartGridView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartGridView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartGridView message handlers

void CChartGridView::SetupLayout()
{
	CBCGPChartExampleView::SetupLayout();

	InitGrid ();

	CBCGPStaticLayout* pLayout = (CBCGPStaticLayout*)GetLayout();
	if (pLayout != NULL)
	{
		pLayout->AddAnchor(ID_GRID, CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);
	}
}

void CChartGridView::UpdateChartColorTheme(int nTheme, BOOL bIsDarkTheme)
{
	CBCGPChartExampleView::UpdateChartColorTheme(nTheme, bIsDarkTheme);
	m_wndGrid.SetVisualManagerColorTheme();
}

void CChartGridView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}
	
	m_bIsReady = TRUE;
}

void CChartGridView::InitGrid ()
{
	ASSERT_VALID (this);
	ASSERT (GetSafeHwnd () != NULL);
	
	CBCGPGridRow::m_strFormatDouble = _T("%.9lg");
	CBCGPGridRow::m_strScanFormatDouble = _T("%lg");

	CRect rectGrid;
	m_wndGridLocation.GetClientRect (&rectGrid);
	m_wndGridLocation.MapWindowPoints (this, &rectGrid);
	
	if (!m_wndGrid.Create (WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER, rectGrid, this, ID_GRID))
	{
		ASSERT(FALSE);
		return;
	}
	
	m_wndGrid.SetOwner(this);
	m_wndGrid.SetVisualManagerColorTheme();
	
	m_wndGrid.EnableHeader (TRUE, BCGP_GRID_HEADER_SELECT);
	m_wndGrid.EnableRowHeader (TRUE, BCGP_GRID_HEADER_SELECT);
	m_wndGrid.EnableLineNumbers (TRUE);

	const int nColumns = 7;

	for (int nColumn = 0; nColumn < nColumns; nColumn++)
	{
		BOOL bIsChartColumn = (nColumn == nColumns - 1);

		CString strColumn;
		strColumn.Format (_T("%c"), _T('A') + nColumn);

		m_wndGrid.InsertColumn(nColumn, strColumn, globalUtils.ScaleByDPI(bIsChartColumn ? 200 : 50));
		m_wndGrid.SetColumnAlign(nColumn, bIsChartColumn ? HDF_CENTER : HDF_RIGHT);
	}
	
	for (int nRow = 0; nRow < 10; nRow++)
	{
		CBCGPGridRow* pRow = m_wndGrid.CreateRow (m_wndGrid.GetColumnCount ());
		CBCGPDoubleArray arData;
		
		for (int i = 0; i < m_wndGrid.GetColumnCount () - 1; i++)
		{
			const double dblVal = bcg_double_precision(Rand(0.0, 200.0), 2);

			pRow->GetItem (i)->SetValue(dblVal);
			arData.Add(dblVal);
		}
		
		CBCGPGridSparklineItem* pItem = new CBCGPGridSparklineItem(arData);
		
		CBCGPChartVisualObject* pChart = pItem->GetChart();
		ASSERT_VALID(pChart);
		
		pChart->ShowAxis(BCGP_CHART_X_PRIMARY_AXIS);
		pChart->ShowAxis(BCGP_CHART_Y_PRIMARY_AXIS);
		
		pChart->GetChartAreaFormat().SetContentPadding(CBCGPSize(
			2.0 * pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS)->GetMajorTickMarkLen(), 
			2.0 * pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS)->GetMajorTickMarkLen()));

		pRow->ReplaceItem(m_wndGrid.GetColumnCount() - 1, pItem);

		m_wndGrid.AddRow (pRow, FALSE);

		pItem->OnAfterChangeGridColors();
	}

	m_wndGrid.AdjustLayout();
}

void CChartGridView::OnChartType() 
{
	UpdateData();

	CWaitCursor wait;

	for (int nRow = 0; nRow < m_wndGrid.GetRowCount(); nRow++)
	{
		CBCGPGridRow* pRow = m_wndGrid.GetRow(nRow);
		ASSERT_VALID(pRow);

		CBCGPGridSparklineItem* pChartItem = DYNAMIC_DOWNCAST(CBCGPGridSparklineItem, pRow->GetItem(m_wndGrid.GetColumnCount() - 1));
		if (pChartItem != NULL)
		{
			ASSERT_VALID(pChartItem);

			pChartItem->SetType((CBCGPGridSparklineItem::SparklineType)m_nChartType);
			pChartItem->Redraw();
		}
	}
}

LRESULT CChartGridView::OnGridDataChanged (WPARAM wParam, LPARAM lParam)
{
	if ((int)wParam == ID_GRID)
	{
		BCGPGRID_ITEM_INFO* pII = (BCGPGRID_ITEM_INFO*)lParam;
		ASSERT(pII != NULL);

		CBCGPGridRow* pRow = m_wndGrid.GetRow(pII->nRow);
		ASSERT_VALID(pRow);

		CBCGPGridItem* pItem = pRow->GetItem(pII->nCol);
		ASSERT_VALID(pItem);

		CBCGPGridSparklineItem* pChartItem = DYNAMIC_DOWNCAST(CBCGPGridSparklineItem, pRow->GetItem(m_wndGrid.GetColumnCount() - 1));
		if (pChartItem != NULL)
		{
			ASSERT_VALID(pChartItem);

			pChartItem->UpdateDataPoint(pII->nCol, (double)pItem->GetValue());
			pChartItem->Redraw();
		}
	}

	return 0;
};

void CChartGridView::OnUpdateUnusedCommand(CCmdUI* pCmdUI)
{
	pCmdUI->Enable(FALSE);
}
